home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / FWTaskG / Sources / FWLookup.asm < prev    next >
Encoding:
Assembly Source File  |  1994-04-21  |  5.7 KB  |  256 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWLookup.asm
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/25/94
  7. //
  8. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.     
  12.     LOCALS            ; Yes, we want local symbols on (those prefixed with a @@)
  13.     Model    Large    ; Large memory model
  14.     
  15.     %NOINCL
  16.     include    macros.asm
  17.  
  18.     public        _FW_BedWinGetTaskGlobals, _FW_BedWinSetTaskGlobals
  19.     extrn        ISTASK:far    ; FAR PASCAL IsTask(HTASK hTask)
  20.         
  21.         begdata
  22.  
  23. kMaxTasks            = 32
  24.  
  25. gCachedTaskID        DW    0
  26. gCachedTaskGPtr        DD    0
  27.  
  28. gTaskCount            DW    0
  29. gTaskIDArray        DW    kMaxTasks DUP (?)
  30. gTaskPtrArray        DD    kMaxTasks DUP (?)
  31.  
  32.         enddata
  33.         
  34.         begcode FWLookup
  35.         
  36. ;========================================================================================
  37. ;    PackTaskTable
  38. ;        Packs the task table, checking for tasks that have terminted
  39. ;        IsTask function is used to check if a task handle is still valid
  40. ;
  41. ;========================================================================================
  42.  
  43. PROC PackTaskTable
  44.  
  45.             WINENTER
  46.             
  47.             mov        cx, [gTaskCount]
  48.             jcxz    @@doReturn
  49.             
  50.             push    SI
  51.             mov        SI, OFFSET gTaskIDArray
  52.             cld                    ; scan forward
  53.             
  54.             ; start the loop
  55.     @@nextTaskID:
  56.             mov        AX, [SI]    ; get the next task ID
  57.             push    AX
  58.             call    ISTASK        ; check if the task is still valid
  59.             jnz        @@doNextTask
  60.             
  61.         ; the current task is not valid, remove the entry
  62.             dec        [gTaskCount]
  63.             dec        CX
  64.  
  65.             mov        DI, [gTaskCount]
  66.             shl        DI, 2
  67.             
  68.             mov        AX, [DI]
  69.             mov        [SI], AX
  70.             mov        AX, [DI + 2]
  71.             mov        [SI + 2], AX
  72.             
  73.             jmp        short @@doLoop    ; loop to the next, but don't increment the pointer
  74.         
  75.         ; flush the cache
  76.             mov        [gCachedTaskID], 0
  77.             
  78.     @@doNextTask:
  79.             inc        SI
  80.             inc        SI
  81.     @@doLoop:        
  82.             loop    @@nextTaskID
  83.             
  84.             pop        SI
  85.             
  86.     @@doReturn:            
  87.             WINLEAVE
  88.             retf
  89.  
  90. PackTaskTable ENDP
  91.  
  92. ;========================================================================================
  93. ;  FW_PrivWinGetTaskGlobals
  94. ;    Returns the global pointer associated with this task
  95. ;
  96. ;    Parameters:
  97. ;        None
  98. ;
  99. ;    Return value:        
  100. ;        The data pointer associated with the current task
  101. ;        If the current task is not in the table, a new item is allocated
  102. ;        The initial value for the data pointer is NULL
  103. ;        If a new item could not be allocated, the return value is -1
  104. ;========================================================================================
  105.  
  106. _FW_BedWinGetTaskGlobals:
  107.  
  108.             ; get the task ID, placed at the top to improve caching on i486
  109.             mov        AX, SS
  110.  
  111. if LPTR
  112.             ; establish the DS
  113.             mov        DX, _DATA
  114.             push    DS
  115.             mov        DS, DX
  116. endif            
  117.                         
  118.             ; see if the task ID is the same as the cached one
  119.             cmp        AX, WORD PTR [gCachedTaskID]
  120.             jne        @@doSearch
  121.             
  122.             ; it is, just return the cached value
  123.             mov        AX, WORD PTR [gCachedTaskGPtr]
  124.             mov        DX, WORD PTR [gCachedTaskGPtr + 2]
  125.             
  126.             ; restore the DS and return
  127.             ; do not jump to the end: that flushes the instruction cache
  128. if LPTR
  129.             pop        DS
  130. endif            
  131.             retf
  132.             
  133.             ; search the table
  134.     @@doSearch:
  135.             push    DI                ; routines need to preserve SI and DI
  136.             
  137.             mov        DX, DS            ; scasw buffer is pointed by ES:DI
  138.             mov        CX, [gTaskCount]
  139.             mov        ES, DX
  140.             mov        DI, OFFSET gTaskIDArray
  141.             cld                        ; search forward
  142.             repnz    scasw
  143.             jnz        @@addNewTaskId
  144.             
  145.             ; the task is in the table, and DI points one past the entry
  146.             mov        gCachedTaskID, AX
  147.             sub        DI, OFFSET gTaskIDArray + 2
  148.             shl        DI, 2
  149.             mov        AX, WORD PTR ES:gTaskPtrArray[DI]
  150.             mov        DX, WORD PTR ES:gTaskPtrArray + 2[DI]
  151.             jmp        short @@cacheAndReturn
  152.                         
  153.             ; the task is not in the table, add a new one
  154.     @@addNewTaskId:        
  155.             cmp        [gTaskCount], kMaxTasks
  156.             jne        @@nowAddTask
  157.             call    PackTaskTable
  158.             cmp        [gTaskCount], kMaxTasks
  159.             je        @@tooManyTasks
  160.             
  161.     @@nowAddTask:
  162.             mov        ES:[DI], AX
  163.             mov        gCachedTaskID, AX
  164.             sub        DI, OFFSET gTaskIDArray
  165.             shl        DI, 2
  166.             xor        AX, AX            ; the initial data pointer value is 0
  167.             mov        DX, AX
  168.             mov        WORD PTR ES:gTaskPtrArray[DI], AX
  169.             mov        WORD PTR ES:gTaskPtrArray + 2[DI], DX
  170.             inc        [gTaskCount]
  171.             jmp        short @@cacheAndReturn
  172.         
  173.             ; too many tasks, return ((void*) -1)
  174.     @@tooManyTasks:
  175.             xor        AX, AX
  176.             dec        AX
  177.             mov        DX, AX
  178.             jmp        short @@returnAXDX
  179.             
  180.             ; store the value in the cache and return
  181.     @@cacheAndReturn:            
  182.             mov        WORD PTR [gCachedTaskGPtr], AX
  183.             mov        WORD PTR [gCachedTaskGPtr + 2], DX
  184.  
  185.             ; restore registers
  186.     @@returnAXDX:            
  187.             pop        DI
  188. if LPTR
  189.             pop        DS
  190. endif            
  191.  
  192.             ; return
  193.             retf
  194.  
  195. ;========================================================================================
  196. ;  FW_BedWinSetTaskGlobals:
  197. ;    Sets the global pointer associated with this task
  198. ;
  199. ;    Parameters:
  200. ;        The data pointer to associate with the current task
  201. ;
  202. ;    Return value:        
  203. ;        1 if everything is kosher
  204. ;        0 if the current task is not in the table yet
  205. ;========================================================================================
  206.  
  207. _FW_BedWinSetTaskGlobals PROC
  208.     ARG dataArg:DWORD
  209.  
  210.             WINENTER
  211.             
  212.             ; save registers
  213.             push    DI
  214.             
  215.             ; BX holds the return value
  216.             xor        BX, BX
  217.             
  218.             ; get the task ID
  219.             mov        AX, SS
  220.         
  221.             mov        CX, [gTaskCount]
  222.             mov        DX, DS            ; scasw buffer is pointed by ES:DI
  223.             mov        ES, DX
  224.             mov        DI, OFFSET gTaskIDArray
  225.             cld                        ; search forward
  226.             repnz    scasw
  227.             jnz        @@doReturn
  228.             
  229.             ; the search is successfull, ES:DI points one past the found entry
  230.             sub        DI, OFFSET gTaskIDArray + 2
  231.             shl        DI, 2
  232.             mov        AX, WORD PTR [dataArg]
  233.             mov        DX, WORD PTR [dataArg + 2]
  234.             mov        WORD PTR ES:gTaskPtrArray[DI], AX
  235.             mov        WORD PTR ES:gTaskPtrArray + 2[DI], DX
  236.  
  237.             ; invalidate the cache
  238.             mov        gCachedTaskID, 0
  239.             
  240.             ; return a value indicating success
  241.             inc        BX
  242.             
  243.             ; restore registers and return
  244.     @@doReturn:            
  245.             pop        DI
  246.             mov        AX, BX        ; put return value into AX
  247.             
  248.             WINLEAVE
  249.             retf
  250.         
  251. _FW_BedWinSetTaskGlobals ENDP
  252.  
  253.         endcode FWLookup
  254.         end
  255.  
  256.